//
//  RegisterVC.m
//  NewNest
//
//  Created by hesiyao on 16/1/15.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "RegisterVC.h"
#import <UIColor+Wonderful.h>
#import "DataBaseManager.h"
@interface RegisterVC () <UITextFieldDelegate>
@property (nonatomic, retain) UIImageView *backImg;
@property (nonatomic, retain) UITextField *userText;
@property (nonatomic, retain) UITextField *passwordText;
@property (nonatomic, retain) UITextField *repasswoedText;
@property (nonatomic, retain) UITextField *mailboxText;
@property (nonatomic, retain) UITextField *phoneText;
@property (nonatomic, retain) UIButton *registerButton;
@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //左侧返回按钮
    self.navigationController.navigationBar.tintColor = [UIColor peachRed];
    
    // 创建视图
    [self createSubviews];
    
}

/// 创建视图方法
- (void)createSubviews
{
    //backImg
    self.backImg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _backImg.image = [UIImage imageNamed:@"registerBackground.jpg"];
    [self.view addSubview:_backImg];
    [_backImg release];
    
    //userText
    self.userText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 19)];
    _userText.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 5);
    _userText.placeholder = @"请输入用户名";
    _userText.clearButtonMode = YES;
    _userText.clearsOnBeginEditing = YES;
    _userText.borderStyle = UITextBorderStyleRoundedRect;
    _userText.delegate = self;
    _userText.tag = 1000;
    [self.view addSubview:_userText];
    [_userText release];
    
    //passwordText
    self.passwordText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, _userText.frame.size.width, _userText.frame.size.height)];
    _passwordText.center = CGPointMake(_userText.center.x, _userText.center.y + _userText.frame.size.height + 15);
    _passwordText.placeholder = @"请输入密码";
    _passwordText.clearButtonMode = YES;
    _passwordText.clearsOnBeginEditing = YES;
    _passwordText.secureTextEntry = YES;
    _passwordText.borderStyle = UITextBorderStyleRoundedRect;
    _passwordText.delegate = self;
    _passwordText.tag = 2000;
    [self.view addSubview:_passwordText];
    [_passwordText release];
    
    //repassWordText
    self.repasswoedText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 19)];
    _repasswoedText.center = CGPointMake(self.view.center.x, _passwordText.center.y + _passwordText.frame.size.height + 15);
    _repasswoedText.placeholder = @"请再次输入密码";
    _repasswoedText.clearButtonMode = YES;
    _repasswoedText.clearsOnBeginEditing = YES;
    _repasswoedText.secureTextEntry = YES;
    _repasswoedText.borderStyle = UITextBorderStyleRoundedRect;
    _repasswoedText.delegate = self;
    _repasswoedText.tag = 3000;
    [self.view addSubview:_repasswoedText];
    [_repasswoedText release];
    
    //mailboxText
    self.mailboxText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 19)];
    _mailboxText.center = CGPointMake(self.view.center.x, _repasswoedText.center.y + _repasswoedText.frame.size.height + 15);
    _mailboxText.placeholder = @"请再次输入密码";
    _mailboxText.clearButtonMode = YES;
    _mailboxText.clearsOnBeginEditing = YES;
    _mailboxText.borderStyle = UITextBorderStyleRoundedRect;
    _mailboxText.delegate = self;
    _mailboxText.tag = 4000;
    [self.view addSubview:_mailboxText];
    [_mailboxText release];
    
    //phoneText
    self.phoneText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 19)];
    _phoneText.center = CGPointMake(self.view.center.x, _mailboxText.center.y + _mailboxText.frame.size.height + 15);
    _phoneText.placeholder = @"请再次输入密码";
    _phoneText.clearButtonMode = YES;
    _phoneText.clearsOnBeginEditing = YES;
    _phoneText.borderStyle = UITextBorderStyleRoundedRect;
    _phoneText.delegate = self;
    _phoneText.tag = 5000;
    [self.view addSubview:_phoneText];
    [_phoneText release];
    
    // registerButton
    self.registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _registerButton.frame = CGRectMake(0, 0, self.view.frame.size.width / 4, self.view.frame.size.height / 19);
    _registerButton.center = CGPointMake(self.view.center.x, _phoneText.center.y + _phoneText.frame.size.height + 15);
    _registerButton.layer.cornerRadius = 5;
    _registerButton.backgroundColor = [UIColor aquamarine];
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [_registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    _registerButton.tintColor = [UIColor peachRed];
    [self.view addSubview:_registerButton];
}

/// 回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 1000) {
        [textField resignFirstResponder];
        [[self.view viewWithTag:2000] becomeFirstResponder];
    }else if (textField.tag == 2000) {
        [textField resignFirstResponder];
        [[self.view viewWithTag:3000] becomeFirstResponder];
    }else if (textField.tag == 3000) {
        [textField resignFirstResponder];
        [[self.view viewWithTag:4000] becomeFirstResponder];
    }else if (textField.tag == 4000) {
        [textField resignFirstResponder];
        [[self.view viewWithTag:5000] becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
    return YES;
}

// 登陆按钮方法实现
- (void)registerAction:(UIButton *)sender
{
    // 如果输入正确就注册成功
    // 先判断输入的格式是不是正确的
    if (self.userText.text.length != 0 && self.passwordText.text.length != 0 && self.repasswoedText.text.length != 0 && self.passwordText.text == self.repasswoedText.text) {
        NSLog(@"格式正确");
        
        // 判断当前的用户名是不是已经存在了, 如果存在就提示已经存在 否则注册
        [[DataBaseManager shareInstance] openDB];
//        NSMutableArray *arr = [[DataBaseManager shareInstance] ]
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
